package com.wuchao.servlet143;

import com.alibaba.fastjson.JSON;
import com.wuchao.entity.Pojo;
import com.wuchao.service.serviceimpl.ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author 吴超66
 * @version 1.0
 * @date 2022/4/26 16:25
 */
@WebServlet(name = "ServletAjax1",urlPatterns = "/ServletAjax1")
public class ServletAjax1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();//文件流
        out.println();

        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();//文件流
        out.println();
      /*  String tt = request.getParameter("tt");
        System.out.println(tt);
        if(tt.equals("143")){
            out.print("y");
        }else{
            out.print("n");
        }*/
        List<Pojo> poJo = new ServiceImpl().findPoJo("select * from t143");
       // String json=[{"id":1,"name":1},....];
       /* StringBuilder sb=new StringBuilder("[");
        for(int i=0;i<poJo.size();i++){
            String xx="{\"id\":\""+poJo.get(i).getId()+"\",\"name\":\""+poJo.get(i).getName()+"\"}";
           sb.append(xx);
            if(i<poJo.size()-1){
                sb.append(",");
            }
        }
        System.out.println(sb);
        sb.append("]");*/
       String sb= JSON.toJSONString(poJo);
        out.println(sb);
        out.flush();
        out.close();
    }
}
